// router.js
import {
    RouterMount,
    createRouter
} from 'uni-simple-router';
import store from './utils/store'
import _routers from './router'
import env from './utils/env.js';
//添加404页面路由，404必须放最后
_routers.push({
    name: '404',
    path: "/common/layout/404",
    aliasPath: '/*',
    meta: {
        //不需要登录
        auth: false
    }
})
const router = createRouter({
    platform: process.env.VUE_APP_PLATFORM,
    h5: {
        loading: true
    },
    routes: _routers
});
//全局路由前置守卫
router.beforeEach((to, from, next) => {
    let isLogin = to.meta.auth == false ? false : true;
    store.commit("router", to);
    store.commit("refresh");
    if (isLogin) {
        if (!store.state.hasLogin) {
            uni.redirectTo({
                url: "/"
            });
            return;
        }
    }
    if (window._hmt) {
        if (to.path) {
            window._hmt.push(['_trackPageview', to.fullPath])
        }
    }
    setTimeout(() => {
        uni.setNavigationBarTitle({
            title: env.title
        });
    }, 100);
    next();
});
// 全局路由后置守卫
router.afterEach((to, from) => {
})

export {
    router,
    RouterMount
}
